package com.symbio.ishenapi.patient.controller;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.patient.model.TmPatientBloodPressure;
import com.symbio.ishenapi.patient.model.TmPatientCount;
import com.symbio.ishenapi.patient.model.TmPatientWeight;
import com.symbio.ishenapi.patient.model.TmPatientWeight;
import com.symbio.ishenapi.patient.model.base.BaseTmPatientBloodPressure;
import com.symbio.ishenapi.patient.model.base.BaseTmPatientWeight;
import io.jboot.web.controller.annotation.RequestMapping;
import org.jsoup.helper.StringUtil;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("/TmPatientWeight")
public class TmPatientWeightController  extends BaseController {

    public void saveTmPatientWeight() {         //体重录入
        TmPatientWeight user = (TmPatientWeight) getReqBodyObject(TmPatientWeight.class);
        TmPatientWeight tmPatientWeight = TmPatientWeight.dao.findOneWeight(user);
        if(tmPatientWeight == null){
            user.setCreateTime(new Date());
            if (user.getWeightValue() == null || "".equals(user.getWeightValue())) {
                renderJsonError(40003, "体重");
            } else if (user.getUserId() == null || "".equals(user.getUserId())) {
                renderJsonError(40003, "患者编号");
            } else if (user.getRuntime() == null || "".equals(user.getRuntime())) {
                renderJsonError(40003, "执行时间");
            } else {
                user.save();
                renderJsonError(40001,"体重");
            }
        }else{
            tmPatientWeight.setWeightValue(user.getWeightValue());
            tmPatientWeight.setUserId(user.getUserId());
            tmPatientWeight.setRuntime(user.getRuntime());
            tmPatientWeight.setStatus("1");
            if(tmPatientWeight.update()){
                renderJsonError(40001,"体重");
            }else{
                renderJsonError(40005);
            }

        }

    }

    /**
     * 患者端请求接口
     */
    public void findTmPatientWeight() {       //查看体重
        TmPatientWeight user = (TmPatientWeight) getReqBodyObject(TmPatientWeight.class);
       if(user.getUserId() == null || "".equals(user.getUserId())){
           renderJsonError(40003, "患者编号");
           return;
       }
        System.out.println(user.getUserId() + "wwwwwwwwww" + user.getId());
        List<TmPatientWeight> list = TmPatientWeight.dao.findTmPatientBloodWeight(user);
        List<TmPatientWeight> weightList = new ArrayList<TmPatientWeight>();
        Date data = new Date();
        List<String[]> mapList = new ArrayList<String[]>();
        String[] arrary = new String[2];
        if (list != null && list.size() > 0) {
            //算出两个时间间隔天数
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = null;
            try {//把runTime转成date
                d = sdf.parse(list.get(list.size()-1).getRuntime());
            }catch (Exception e){
                e.printStackTrace();
            }
            String da = sdf.format(new Date());

            try {//把当前时间转成"yyyy-MM-dd"
                data = sdf.parse(da);
            }catch (Exception e){
                e.printStackTrace();
            }
            int days = (int) ((data.getTime() - d.getTime()) / (1000 * 3600 * 24));
            Map<String, Integer> map = new HashMap<String, Integer>();
            Long time = d.getTime();
            for (int i = 0; i <= days; i++) {
                if (i > 0) {
                    time = time + 86400000;
                }
                //时间戳转Date
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//12小时制
                Date date = new Date();
                date.setTime(time);
                arrary = new String[2];
                arrary[0] = simpleDateFormat.format(date).toString();
                arrary[1] = "0";
                mapList.add(arrary);
            }
        }

        List<String[]> lists = new ArrayList<String[]>();
        for (int k = 0; k < mapList.size(); k++) {
            for (TmPatientWeight tmPatientWeight : list) {
                String[] a = mapList.get(k);
                String createtime = tmPatientWeight.getRuntime();
                if (a[0].equals(createtime)) {//将查出来的数据替换到mapList里
                    a[1] = tmPatientWeight.getWeightValue();
                }
            }
        }
        renderJsonRet(mapList);
    }


    public void updTmPatientWeight() {        //修改体重
        TmPatientWeight user = (TmPatientWeight) getReqBodyObject(TmPatientWeight.class);
        TmPatientWeight u = TmPatientWeight.dao.findById(user.getId());
        if (u != null) {
            if (!StrKit.isBlank(user.getWeightValue())) u.setWeightValue(user.getWeightValue());
            if (!StrKit.isBlank(user.getRuntime())) u.setRuntime(user.getRuntime());
            //if (!StrKit.isBlank(user.getCreateTime().toString())) u.setCreateTime(user.getCreateTime());
            if (u.update()) {
                renderJsonRet(user.getId());
            } else {
                renderJsonError(40005);
            }
        } else {
            renderJsonError(40003);
            return;
        }
    }


    /**
     * wenjun
     * 查询体重当前或最新一条数据
     */
    public void findOneWeight() {
        TmPatientWeight user = (TmPatientWeight) getReqBodyObject(TmPatientWeight.class);
        System.out.println(user.getUserId() + "wwwwwwwwww" + user.getId());
        if (null == user.getUserId() && "".equals(user.getUserId())) {
            renderJsonError(40005);
        }
        if(null==user.getRuntime() || "".equals(user.getRuntime())){
            Date date =new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
            String time = sdf.format(date);
            user.setRuntime(time);
        }
        TmPatientWeight list = TmPatientWeight.dao.findOneWeight(user);
        if(null != list ){
            renderJsonRet(list);
        }else{
            list = new TmPatientWeight();
            renderJsonRet(list);
        }
    }

    /**
     * 根据userId和CreateTime查询数据单一
     */
    public void findUseIdByCreateTime() {
        TmPatientWeight user = (TmPatientWeight) getReqBodyObject(TmPatientWeight.class);
        if (user.getCreateTime() == null || "".equals(user.getCreateTime())) {
            renderJsonError(40003, "时间");
        } else if (user.getUserId() == null || "".equals(user.getUserId())) {
            renderJsonError(40003, "患者编号");
        }
        System.out.println(user.getUserId() + "wwwwwwwwww" + user.getCreateTime());
        List<TmPatientWeight> list = TmPatientWeight.dao.findUserIdByCreateTime(user);
        TmPatientWeight tpw = new TmPatientWeight();
        if (list != null && list.size() > 0) {
            tpw = list.get(0);
        }
        renderJsonRet(tpw);
    }

    /**
     * 管理端请求，查看患者体重
     *
     *
     */
    public void findPatientWeight() {
        String userId = getPara("userId");
        if(userId == null || "".equals(userId)){
            renderJsonError(40003, "患者编号");
            return;
        }
        Map map = TmPatientWeight.dao.findPatientBloodWeight(new BigInteger(userId),getQueryModel());
        renderPageJsonRet(map.get("count"),map.get("data"));

    }
}

